package com.src.socket;


import java.io.*;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

/**
 * Created by shirenchuang on 2017/3/24.
 *
 * 通信demo：服务端
 */
public class SocketServiceT {
    private static  ExecutorService executorService = Executors.newFixedThreadPool(3);

    public static void main(String[] args) throws IOException {
        //singleThread();
        multiThread();
    }

    public static void singleThread(){


        ServerSocket serverSocket = null;
        Socket socket = null;
        InputStream inputStream = null;
        BufferedReader br = null;
        OutputStream outputStream = null;
        PrintWriter pw = null;
        try {
            serverSocket = new ServerSocket(8089);
            //阻塞式接受客户端
            socket = serverSocket.accept();
            inputStream = socket.getInputStream();
            InputStreamReader inputStreamReader = new InputStreamReader(inputStream);
            br = new BufferedReader(inputStreamReader);
            String str;
            StringBuffer sb = new StringBuffer();
            while ((str = br.readLine()) != null) {    //如果之前文件为空，则不执行输出
                sb.append(str);
            }
            System.out.print("我是服务端，客户端 说："+sb.toString());

            //关闭输入流
            socket.shutdownInput();

            //服务端返回 信息
            outputStream = socket.getOutputStream();
            pw = new PrintWriter(outputStream);
            pw.write("你好，我是服务端，我收到了你的信息了");
            pw.flush();

        } catch (IOException e) {
            e.printStackTrace();
        }finally {
            pw.close();
            try {
                outputStream.close();
                br.close();
                inputStream.close();
                socket.close();
                serverSocket.close();
            } catch (IOException e) {
                e.printStackTrace();
            }


        }

    }



    public static void multiThread() throws IOException {
        //打开socket服务
        ServerSocket serverSocket = new ServerSocket(8089);;
        try {
            //让它一直监听
            while (true){
                Socket socket = serverSocket.accept();
                executorService.execute(new Task(socket));
            }
        }finally {
            serverSocket.close();
            System.out.print("end.......");
        }

    }


    //执行 每个线程 的任务

    private   static class  Task implements Runnable{

        Socket socket = null;
        public Task(Socket socket){
            this.socket = socket ;
        }

        public void run() {
            InputStream inputStream = null;
            InputStreamReader inputStreamReader = null ;
            BufferedReader br = null;
            OutputStream outputStream = null;
            PrintWriter pw = null;
            try {
                //阻塞

                inputStream = socket.getInputStream();
                inputStreamReader = new InputStreamReader(inputStream);
                br = new BufferedReader(inputStreamReader);
                String str;
                StringBuffer sb = new StringBuffer();
                while ((str = br.readLine()) != null) {    //如果之前文件为空，则不执行输出
                    sb.append(str);
                }
                System.out.print("我是服务端，客户端 说："+sb.toString());


                //关闭输入流
                socket.shutdownInput();

                //服务端返回 信息
                outputStream = socket.getOutputStream();
                pw = new PrintWriter(outputStream);
                pw.write("你好，我是服务端，我收到了你的信息了");
                pw.flush();
            } catch (IOException e) {
                e.printStackTrace();
            }finally {
                pw.close();
                try {
                    outputStream.close();
                    br.close();
                    inputStream.close();
                    socket.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }

            }
        }
    }


}
